const express = require('express')
const router = express.Router()
const { UserFootPrint } = require('../model/index')

router.post('/storeinfo', (req, res) => {
  const info = req.body
  const { username } = req.body
  new UserFootPrint({ commodity: info, username }).save()
    .then(resp => {
      res.json({
        code: 200,
        data: resp
      })
    }).catch(err => {
      res.json({
        code: 400,
        data: err
      })
    })
})

router.post('/getinfo', (req, res) => {
  const { username } = req.body
  UserFootPrint.find({ username }).then(resp => {
    res.json({
      code: 200,
      data: resp
    })
  }).catch(err => {
    res.json({
      code: 400,
      data: err
    })
  })
})

router.post('/delinfo', (req, res) => {
  const {_id} = req.body
  UserFootPrint.deleteOne({_id})
    .then(resp => {
      res.json({
        code: 200,
        data: resp
      })
    }).catch(err => {
      res.json({
        code: 400,
        data: err
      })
    })
})

module.exports = router